# -*- encoding: utf-8 -*-
"""
File Name: network_device_asset
Description:
Author : 'lirui'
date: 2024/2/21
Contact: marjey_lee@163.com
Change Activity:
2024/2/21:
"""
from modules.blk.cmdb.common.cmdb_api import CMDBAPI


class NetworkDeviceAssetService:
    KEY = 'network_device_asset'

    def __init__(self):
        self.data = self.refresh_data()

    @staticmethod
    def refresh_data():
        data = CMDBAPI().select_all(NetworkDeviceAssetService.KEY)
        data = data['data']['info']
        mapping = {}
        for d in data:
            mapping[d['bk_inst_name']] = d
        return mapping

    def get_data(self, ip):
        """

        Returns:

        """
        if ip in self.data:
            return self.data[ip]
        return None

    def get_hostname_by_ip(self, ip):
        data = self.get_data(ip)
        if data is not None:
            return data['equipment_name']
        return None


NETWORK_DEVICE_ASSET_SERVER = NetworkDeviceAssetService()
